package com.example.memorypoint.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.memorypoint.DateBase.DiaryDatabase;
import com.example.memorypoint.R;
import com.example.memorypoint.adapter.DetailAdapter;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.yanzhenjie.recyclerview.OnItemClickListener;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.ArrayList;


@SuppressLint("ViewConstructor")
public class DesignDetailHelper extends BottomPopupView implements View.OnClickListener {

    private SwipeRecyclerView detail_recyclerView;
    private TextView tv_cancel;
    private TextView tv_submit;
    private int screenWidth;
    private TextView tv_detail;
    private TextView detailView;
    private ArrayList<DetailAdapter.Data> weatherResource = null;
    private Context mContext;
    private String[] name = null;
    private DetailAdapter adapter;
    private String detail;
    private int location;
    private DiaryDatabase database;
    private int diaryID;
    private ImageView weather;
    private ImageView emoji;

    public DesignDetailHelper(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.layout_detail_recycleview;
    }


    public DesignDetailHelper(@NonNull Context context, ArrayList<DetailAdapter.Data> weatherResource, Context mContext, String[] name, String detail, int diary, ImageView weather, ImageView emoji) {
        super(context);
        this.weatherResource = weatherResource;
        this.mContext = mContext;
        this.name = name;
        this.detail = detail;
        this.diaryID = diary;
        this.weather = weather;
        this.emoji = emoji;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        initView();
        initData();
        database = new DiaryDatabase(mContext);

    }

    @SuppressLint("SetTextI18n")
    private void initData() {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
        detail_recyclerView.setSwipeMenuCreator(swipeMenuCreator);
        detail_recyclerView.setOnItemClickListener(itemClickListener);
        adapter = new DetailAdapter(weatherResource, screenWidth);
        RecyclerView.LayoutManager manager = createLayoutManager();
        detail_recyclerView.setLayoutManager(manager);
        detail_recyclerView.setAdapter(adapter);
    }

    @SuppressLint("WrongViewCast")
    private void initView() {
        detail_recyclerView = findViewById(R.id.detail_recyclerView);
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_submit = findViewById(R.id.tv_submit);
        tv_detail = findViewById(R.id.tv_detail);
        detailView = findViewById(R.id.weather);
        tv_cancel.setOnClickListener(this);
        tv_submit.setOnClickListener(this);
    }

    //控制每行显示的数量
    protected RecyclerView.LayoutManager createLayoutManager() {
        return new GridLayoutManager(mContext, 3);
    }

    /**
     * 菜单创建器，在Item要创建菜单的时候调用。
     */


    private final SwipeMenuCreator swipeMenuCreator = (swipeLeftMenu, swipeRightMenu, viewType) ->
    {

    };
    private final OnItemClickListener itemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(View view, int adapterPosition) {

            tv_detail.setText(name[adapterPosition]);
            detailView.setBackground(weatherResource.get(adapterPosition).getResource());
            detail_recyclerView.setAdapter(adapter);
            location = adapterPosition;
            Toast.makeText(mContext, name[adapterPosition], Toast.LENGTH_SHORT).show();
        }
    };

    //完全可见执行
    @Override
    protected void onShow() {
        super.onShow();
    }

    //完全消失执行
    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getScreenHeight(getContext()) * .7f);
    }

    @Override
    protected boolean onBackPressed() {
        Toast.makeText(getContext(), "拦截返回", Toast.LENGTH_SHORT).show();
        return true;
    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_cancel) {
            dismiss();
        } else if (view.getId() == R.id.tv_submit) {
            if (detail.equals("天气")) {
                database.updateWeather(diaryID, String.valueOf(location));
            }
            if (detail.equals("表情")) {
                database.updateEmoji(diaryID, String.valueOf(location));
            }
            String emojiId = database.getEmojiById(diaryID);
            if (detail.equals("表情") && !emojiId.equals("")) {
                if (emojiId != null)
                    emoji.setImageDrawable(weatherResource.get(Integer.parseInt(emojiId)).getResource());
            }
            String weatherId = database.getWeatherById(diaryID);
            if (detail.equals("天气") && !weatherId.equals("")) {
                if (weatherId != null)
                    weather.setImageDrawable(weatherResource.get(Integer.parseInt(weatherId)).getResource());
            }
            this.dismiss();
        }
    }


}